package team.knowers.qzh.qzhserver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.poi.hpsf.Section;
import org.springframework.stereotype.Service;
import team.knowers.qzh.qzhserver.dao.entity.CourseDO;
import team.knowers.qzh.qzhserver.dao.entity.MyCourseDO;
import team.knowers.qzh.qzhserver.dao.entity.SectionDO;
import team.knowers.qzh.qzhserver.dao.entity.UserDO;
import team.knowers.qzh.qzhserver.dao.mapper.MyCourseMapper;
import team.knowers.qzh.qzhserver.dto.req.ChangeProgressReqDTO;
import team.knowers.qzh.qzhserver.dto.req.SetMyCourseReqDTO;
import team.knowers.qzh.qzhserver.dto.resp.MyCourseRespDTO;
import team.knowers.qzh.qzhserver.service.CourseService;
import team.knowers.qzh.qzhserver.service.MyCourseService;
import team.knowers.qzh.qzhserver.service.SectionService;
import team.knowers.qzh.qzhserver.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xtg
 */
@Service
@RequiredArgsConstructor
public class MyCourseServiceImpl extends ServiceImpl<MyCourseMapper, MyCourseDO> implements MyCourseService {
    private final CourseService courseService;
    private final UserService userService;
    private final SectionService sectionService;

    @Override
    public List<MyCourseRespDTO> listMyCourse(String username) {
        LambdaQueryWrapper<MyCourseDO> queryWrapper = Wrappers.lambdaQuery(MyCourseDO.class).eq(MyCourseDO::getDelFlag, 0).eq(MyCourseDO::getUsername, username);
        List<MyCourseDO> myCourseDOS = baseMapper.selectList(queryWrapper);
        List<MyCourseRespDTO> results = new ArrayList<>();
        for (MyCourseDO one : myCourseDOS) {
            MyCourseRespDTO result = new MyCourseRespDTO();
            result.setId(one.getCourseId());
            result.setProgress(one.getProgress());
            if (one.getProgress() == 0L) {
                result.setSectionName("还未开始学习");
            } else {
                LambdaQueryWrapper<SectionDO> eq = Wrappers.lambdaQuery(SectionDO.class).eq(SectionDO::getId, result.getProgress()).eq(SectionDO::getDelFlag, 0);
                SectionDO sectionDO = sectionService.getBaseMapper().selectOne(eq);
                result.setSectionName(sectionDO.getName());
            }
            LambdaQueryWrapper<CourseDO> eq = Wrappers.lambdaQuery(CourseDO.class).eq(CourseDO::getDelFlag, 0).eq(CourseDO::getId, one.getCourseId());
            CourseDO one1 = courseService.getBaseMapper().selectOne(eq);
            result.setCourseName(one1.getName());
            result.setCourseCover(one1.getCover());
            UserDO byId = userService.getById(one1.getUserId());
            result.setCourseAuthor(byId.getNickname());
            results.add(result);
        }
        return results;
    }

    @Override
    public Boolean setMyCourse(SetMyCourseReqDTO reqDTO) {
        MyCourseDO build = MyCourseDO.builder().courseId(reqDTO.getCourseId())
                .username(reqDTO.getUsername())
                .progress(0L).build();
        baseMapper.insert(build);
        courseService.addSalesVolume(reqDTO.getCourseId());
        return true;
    }

    @Override
    public Boolean isMyCourse(SetMyCourseReqDTO reqDTO) {
        LambdaQueryWrapper<MyCourseDO> eq = Wrappers.lambdaQuery(MyCourseDO.class).eq(MyCourseDO::getUsername, reqDTO.getUsername())
                .eq(MyCourseDO::getCourseId, reqDTO.getCourseId())
                .eq(MyCourseDO::getDelFlag, 0);
        MyCourseDO myCourseDO = baseMapper.selectOne(eq);
        return myCourseDO != null;
    }

    @Override
    public Void changeProgress(ChangeProgressReqDTO reqDTO) {
        LambdaQueryWrapper<MyCourseDO> eq = Wrappers.lambdaQuery(MyCourseDO.class).eq(MyCourseDO::getDelFlag, 0).eq(MyCourseDO::getCourseId, reqDTO.getCourseId()).eq(MyCourseDO::getUsername, reqDTO.getUsername());
        MyCourseDO myCourseDO = baseMapper.selectOne(eq);
        myCourseDO.setProgress(reqDTO.getSectionId());
        LambdaUpdateWrapper<MyCourseDO> eq1 = Wrappers.lambdaUpdate(MyCourseDO.class).eq(MyCourseDO::getDelFlag, 0).eq(MyCourseDO::getCourseId, reqDTO.getCourseId()).eq(MyCourseDO::getUsername, reqDTO.getUsername());
        baseMapper.update(myCourseDO, eq1);
        return null;
    }

    public Long getProgress(Long courseId, String username) {
        LambdaQueryWrapper<MyCourseDO> eq = Wrappers.lambdaQuery(MyCourseDO.class).eq(MyCourseDO::getDelFlag, 0).eq(MyCourseDO::getCourseId, courseId).eq(MyCourseDO::getUsername, username);
        MyCourseDO myCourseDO = baseMapper.selectOne(eq);
        return myCourseDO.getProgress();
    }
}
